#include "lua/lua.hpp"
#include <cstring>
static char* cpp_function(const char* c)
{
    size_t sz = strlen(c);
    char * tmp = new char[strlen(c)+1];
    tmp[sz] = '\0';
    for ( int i=0; i<strlen(c); i++)
    {
        tmp[sz-i-1] = c[i];
    }
    return tmp;
}

extern "C" int lua_facede(lua_State *L)
{
    int src_name_len, tmp_name_len;
    const char *names[2];

    int n = lua_gettop(L);
    if ( n < 2 )
    {
        lua_pushstring(L, "必须参数数量不足");
        lua_error(L);
    }

    for ( int i=0; i<2; i++)
    {
        if (!lua_isstring(L, -i-1))
        {
            lua_pushstring(L, "参数类型不正确：期望字符串类型");
            lua_error(L);
        }
        names[i] = lua_tostring(L, -i-1);
    }

    lua_pushstring(L, cpp_function(names[1]));
    lua_pushstring(L, cpp_function(names[0]));
    return 2;
}

static const luaL_Reg luaLibs[] = {
    { "test", lua_facede },
    { NULL, NULL }
};


extern "C" int luaopen_lua_link(lua_State* L )
{
    luaL_newlib(L, luaLibs);
    return 1;
}
